/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf;

import ch.qos.logback.classic.Level;
import io.iec.edp.caf.commons.runtime.CafEnvironment;
import io.iec.edp.caf.entity.ServerConfig;
import io.iec.edp.caf.entity.StartupConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 启动信息输出
 * @author wangyandong
 * @date 2019/9/23 16:59
 * @email wangyandong@inspur.com
 */
public class StartupInfoLogger {
    private static final Logger logger = LoggerFactory.getLogger(StartupInfoLogger.class);
    static void logStarted(ConfigurableListableBeanFactory beanFactory){
        List<String> messages = getStartedMessage(beanFactory);
        double uptime = (double) ManagementFactory.getRuntimeMXBean().getUptime() / 1000.0D;
        Boolean enableConsoleLog = CafEnvironment.getEnvironment().getProperty("ENABLE_CONSOLE_LOGGING",Boolean.class,true);
        if(messages!=null) {
            String ports = getPorts(beanFactory);
            for (String item : messages) {
                String s = item.replaceAll("\\{port\\}", ports)
                        .replaceAll("\\{uptime\\}", String.valueOf(uptime));

                //如果不允许写info日志，则强制打开并输出
                if (!logger.isInfoEnabled()) {
                    ch.qos.logback.classic.LoggerContext loggerContext = (ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
                    ch.qos.logback.classic.Logger loggerInLog4back = loggerContext.getLogger(StartupInfoLogger.class);
                    Level originLevel = loggerInLog4back.getLevel();
                    // 按传递的包名或类名修改日志级别。
                    loggerInLog4back.setLevel(Level.INFO);
                    loggerInLog4back.info(s);
                    loggerInLog4back.setLevel(originLevel);
                } else {
                    logger.info(s);
                }

                //如果禁止输出控制台日志，则直接输出一下
                if (!enableConsoleLog) {
                    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS ");
                    System.out.println(f.format(new Date()) + s);
                }
            }
        }
    }

    public static String getPorts(ConfigurableListableBeanFactory beanFactory) {
        return  beanFactory.getBean(ServerConfig.class).getPorts();
    }

    public static List<String> getStartedMessage(ConfigurableListableBeanFactory beanFactory){
        return  beanFactory.getBean(StartupConfigurationProperties.class).getMessages();
    }
}
